package me.work.pay.jsyl.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.yzy.voice.ReturnPayResult;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.work.pay.jsyl.R;
import me.work.pay.jsyl.app.Constants;
import me.work.pay.jsyl.app.utils.QRCodeUtil;
import me.work.pay.jsyl.di.component.DaggerPayComponent;
import me.work.pay.jsyl.mvp.contract.PayContract;
import me.work.pay.jsyl.mvp.model.AppModel;
import me.work.pay.jsyl.mvp.model.entity.PayData;
import me.work.pay.jsyl.mvp.presenter.PayPresenter;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * 调起支付页面
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 09/12/2020 16:02
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class PayLandActivity extends BaseActivity<PayPresenter> implements PayContract.View {

    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.iv)
    ImageView iv;
    @BindView(R.id.shop_name_tv)
    TextView shopNameTv;
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {

        DaggerPayComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }


    @Override
    public int initView(@Nullable Bundle savedInstanceState) {

            return R.layout.activity_pay_land; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    public String now_pay_num = "";//当前要支付的金额
    private void setText(){
        Log.e("now_pay_num",now_pay_num);
        if(TextUtils.isEmpty(now_pay_num)||now_pay_num==null){
            tv1.setText("");
        }else {
            tv1.setText(now_pay_num);
            mPresenter.load_pay(now_pay_num);
        }
    }
    public static PayLandActivity main;
    int debug_num=0;

    @Override
    protected void onResume() {
        super.onResume();
        init_page();
    }
    private void init_page(){
        SPUtils.getInstance().put(Constants.sp_page_value,"2");
    }
    @Subscribe
    public void onEvent(ReturnPayResult result) {
        if(TextUtils.isEmpty(result.money)){
            return;
        }
        load_money(result.money);
    }
    private void load_money(String money){
        now_pay_num=money;
        Log.e("sea:now_pay_num",now_pay_num);
        if(now_pay_num.equals("741852")){
            debug_num++;
            if(debug_num>2){
                open_debug();
            }
        }
        setText();
    }
    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        main=this;
        init_page();
        EventBus.getDefault().register(this);
        String money=getIntent().getStringExtra("money");
        load_money(money);
//        if(!TextUtils.isEmpty(now_pay_num)){
//            mPresenter.load_pay(now_pay_num);
//        }
        shopNameTv.setText(SPUtils.getInstance().getString(Constants.sp_shop_name));
    }

    private void open_debug(){
        //  (1 :打开adb调试，2：关闭adb调试工具)
        boolean  enableAdb = (Settings.Secure.getInt(getContentResolver(),Settings.Secure.ADB_ENABLED,  0 ) >  0 );
        if  (!enableAdb) {
            Settings.Secure.putInt(getContentResolver(), Settings.Secure.ADB_ENABLED, 1 );
        }
    }

    @Override
    public void load_pay(PayData data) {
        Bitmap bitmap=QRCodeUtil.createQRCodeBitmap(data.qr_code_url,270,270);
        iv.setImageBitmap(bitmap);
    }
    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public Activity getActivity() {
        return this;
    }


}
